package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.HeadingNews;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.request.banner.QueryHeadingRequest;
import com.dianxin.farm.request.banner.TopRequest;
import com.dianxin.farm.request.common.IdRequest;
import com.dianxin.farm.service.CurrentLoginService;
import com.dianxin.farm.service.IHeadingNewsService;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 头条新闻 前端控制器
 * </p>
 *
 * @author xukai
 * @since 2020-09-14
 */
@RestController
@RequestMapping(value = "/headingNews",method = RequestMethod.POST, produces = "application/json;charset=utf-8")
public class HeadingNewsController {

    @Resource
    private IHeadingNewsService headingNewsService;
    @Resource
    private CurrentLoginService currentLoginService;

    @ApiOperation("发布或者头条信息，发布时候id请传0或者不传。默认为草稿形态")
    @RequestMapping(value = "/publishNews")
    @ResponseBody
    @AuthorCheck
    public ApiResponse publishNews(@RequestBody HeadingNews request) {
        Integer creatorId = currentLoginService.getId();
        request.setCreatorId(creatorId);
        Integer id=request.getId();
        Integer status=request.getStatus();
        if (id==null || id ==0){
            request.setId(0);
            request.setCreateTime(LocalDateTime.now());
        }
        if (status==1){
            request.setPublishTime(LocalDateTime.now());
        }
        headingNewsService.saveOrUpdate(request);
        return ApiResponseUtils.success();
    }




    @ApiOperation("头条分页列表,按发布时间和置顶排序")
    @RequestMapping(value = "/headingPage")
    @ResponseBody
    @AuthorCheck
    public ApiResponse<PageInfo<HeadingNews>> headingPage(@Valid @RequestBody QueryHeadingRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        Integer status = request.getStatus();
        Integer topFlag = request.getTopFlag();
        String heading = request.getHeading();
        Date start=request.getStartTime();
        Date end=request.getEndTime();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(status != null, "status", status);
        wrapper.eq(topFlag != null, "top_flag", topFlag);
        wrapper.gt(start != null, "publish_time", start);
        wrapper.lt(end != null, "publish_time", end);
        if (heading != null && !heading.equals("")) {
            wrapper.like("heading", heading);
        }

        wrapper.orderByDesc("publish_time","top_flag");

        List<HeadingNews> newsList = headingNewsService.list(wrapper);
        return ApiResponseUtils.success(new PageInfo<>(newsList));
    }

    @ApiOperation("删除头条消息")
    @RequestMapping(value = "/remove")
    @ResponseBody
    @AuthorCheck
    public ApiResponse remove(@Valid @RequestBody IdRequest request) {
        Integer id=request.getId();
        headingNewsService.removeById(id);
        return ApiResponseUtils.success();
    }



    @ApiOperation("修改置顶状态")
    @RequestMapping(value = "/editTop")
    @ResponseBody
    @AuthorCheck
    public ApiResponse editTop(@Valid @RequestBody TopRequest request) {
        Integer id=request.getHeadingId();
        Boolean top=request.getTopFlag();

        HeadingNews headingNews=headingNewsService.getById(id);
        if (headingNews!=null){
            headingNews.setTopFlag(top);
            if (top.equals(Boolean.TRUE)){
                headingNews.setPublishTime(LocalDateTime.now());
            }
            headingNewsService.updateById(headingNews);
        }
        return ApiResponseUtils.success();
    }

    @ApiOperation("修改heading状态---发布")
    @RequestMapping(value = "/pub")
    @ResponseBody
    @AuthorCheck
    public ApiResponse pub(@Valid @RequestBody IdRequest request) {
        Integer id=request.getId();
        HeadingNews headingNews=headingNewsService.getById(id);
        if (headingNews!=null){
            headingNews.setStatus(1);
            headingNewsService.updateById(headingNews);
        }
        return ApiResponseUtils.success();
    }

    @ApiOperation("修改heading状态---取消发布")
    @RequestMapping(value = "/cancelPub")
    @ResponseBody
    @AuthorCheck
    public ApiResponse cancelPub(@Valid @RequestBody IdRequest request) {
        Integer id=request.getId();
        HeadingNews headingNews=headingNewsService.getById(id);
        if (headingNews!=null){
            headingNews.setStatus(0);
            headingNewsService.updateById(headingNews);
        }
        return ApiResponseUtils.success();
    }




}
